package com.srit.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.srit.core.bean.Page;
import com.srit.forecast.bean.WeatherCode;
import com.srit.system.bean.PageConfig;
import com.srit.system.dao.PageConfigDao;

/**
 * 报文配置CRUD
 * 
 * @author guan
 * 
 */

@Service
public class PageConfigDaoImpl implements PageConfigDao {

	@Autowired(required = true)
	private JdbcTemplate jdbcTemplate;

	@Override
	public void save(PageConfig pageConfig) {
		String sql = "insert into TC_PAGECONFIG (ID,NAME,PAGEVALUE,REMARK) values(SEQ_TC_PAGECONFIG.nextVal,?,?,?)";
		jdbcTemplate.update(sql, new Object[] { pageConfig.getName(),
				pageConfig.getPageValue(), pageConfig.getRemark() });
	}

	@Override
	public void update(PageConfig pageConfig) {
		String sql = "update  TC_PAGECONFIG set NAME=?,PAGEVALUE=?,REMARK=? where id=?";
		jdbcTemplate.update(sql,
				new Object[] { pageConfig.getName(), pageConfig.getPageValue(),
						pageConfig.getRemark(), pageConfig.getId() });
	}

	@Override
	public PageConfig getById(Integer id) {
		String sql = "select  * from TC_PAGECONFIG  where id=?";
		return jdbcTemplate.queryForObject(sql, new Object[] { id },
				new PageConfigRowMapper());
	}

	@Override
	public void page(Page page) {
		String sqlCount = "select count(*) from TC_PAGECONFIG where 1=1 ";
		String sqlAnd = " ";
		String sqlSelect = "select * "
				+ "from (select rownum r,t1.* from TC_PAGECONFIG t1 where rownum<? "
				+ sqlAnd + ") t2 where t2.r>?";
		int count = jdbcTemplate.queryForInt(sqlCount + sqlAnd);
		List<WeatherCode> list = jdbcTemplate.query(sqlSelect, new Object[] {
				(page.getPageNum() + 1) * page.getPageSize(),
				page.getPageNum() * page.getPageSize() },
				new PageConfigRowMapper());
		page.setCount(count);
		page.setList(list);
	}

	@Override
	public int getMaxId() {
		String sql = "select max(ID) from TC_PAGECONFIG";
		return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<PageConfig> listAll() {
		String sql = "select * from TC_PAGECONFIG";
		return jdbcTemplate.query(sql, new PageConfigRowMapper());
	}

}

class PageConfigResultSetExtractor implements ResultSetExtractor {

	@Override
	public Object extractData(ResultSet rs) throws SQLException,
			DataAccessException {
		PageConfig pc = new PageConfig();
		pc.setId(rs.getInt("ID"));
		pc.setRemark(rs.getString("REMARK"));
		pc.setPageValue(rs.getString("PAGEVALUE"));
		pc.setName(rs.getString("NAME"));
		return pc;
	}

}

class PageConfigRowMapper implements RowMapper {
	@Override
	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		PageConfigResultSetExtractor extractor = new PageConfigResultSetExtractor();
		return extractor.extractData(rs);
	}

}
